﻿using INTEX.SCHEDULER;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public class VMAPS : ViewModelBase
    { 
         private string _RENR; public string RENR { get { return _RENR; } set { _RENR = value; OnPropertyChanged("RENR", value); } }
        private string _LIEFSCH; public string LIEFSCH { get { return _LIEFSCH; } set { _LIEFSCH = value; OnPropertyChanged("LIEFSCH", value); } }
        private string _VKP; public string VKP { get { return _VKP; } set { _VKP = value; OnPropertyChanged("VKP", value); } }
        private string _VART; public string VART { get { return _VART; } set { _VART = value; OnPropertyChanged("VART", value); } }
        private string _AUF; public string AUF { get { return _AUF; } set { _AUF = value; OnPropertyChanged("AUF", value); } }

         private string _ABKRS; public string ABKRS { get { return _ABKRS; } set { _ABKRS = value; OnPropertyChanged("ABKRS", value); } }
        private string _ABLFD; public string ABLFD { get { return Helper.Right("000000" + _ABLFD, 6); } set { _ABLFD = value; OnPropertyChanged("ABLFD", value); } }
        private string _ABPOS; public string ABPOS { get { return Helper.Right("000" + _ABPOS, 3); ; } set { _ABPOS = value; OnPropertyChanged("ABPOS", value); } }
        private string _MKFTP; public string MKFTP { get { return _MKFTP; } set { _MKFTP = value; OnPropertyChanged("MKFTP", value); } }

        private string _KSTKRS; public string KSTKRS { get { return _KSTKRS; } set { _KSTKRS = value; OnPropertyChanged("KSTKRS", value); } }
        private string _KST; public string KST { get { return _KST; } set { _KST = value; OnPropertyChanged("KST", value); } }
        private string _AGANG; public string AGANG { get { return _AGANG; } set { _AGANG = value; OnPropertyChanged("AGANG", value); } }
        private string _AMTYP; public string AMTYP { get { return _AMTYP; } set { _AMTYP = value; OnPropertyChanged("AMTYP", value); } }
        private string _JOBTYPE; 
        public string JOBTYPE
        { 
            get 
            { 
                return _JOBTYPE;
            } 
            set 
            { 
                _JOBTYPE = value; 
                OnPropertyChanged("JOBTYPE");
            }
        }
      
        VABPOS VABPOS = new VABPOS();
        VICC_MKRF VICC_MKRF = new VICC_MKRF();
        VICC_PVKPOS VICC_PVKPOS = new VICC_PVKPOS();
        VICC_JWYARN VICC_JWYARN = new VICC_JWYARN();
        VICC_QUERYTB QTB; 
     
      
        public CollectionView vwMKRF
        {
            get
            {
                return (CollectionView)CollectionViewSource.GetDefaultView(SetEnv.Tables["ICC_MKRF"]);
            }
        }

        private DataRowView _dwMKRF = null;
        public DataRowView dwMKRF
        {
            get
            {
                return _dwMKRF;
            }
            set
            {
                _dwMKRF = value;
                if (value != null)
                {
                    ABKRS = _dwMKRF["ABFAUFKRS"].ToString();
                    ABLFD = _dwMKRF["ABFAUFLFD"].ToString();
                    ABPOS = _dwMKRF["ABFAUFPOS"].ToString();
                }
                OnPropertyChanged("dwMKRF", value);
            }
        }
        private string _MACHINEID; public string MACHINEID { get { return _MACHINEID; } set { _MACHINEID = value; OnPropertyChanged("MACHINEID", value); } }
       
        public DelegateCommand PlanCommand { get; set; }
        public DelegateCommand RefreshCommand { get; set; }
        public DelegateCommand StartScheduleCommand { get; set; }
        public DelegateCommand ShowDetailCommand { get; set; } 
        public VMAPS()
            : base()
        {
            QueryCommand = new DelegateCommand(x => Query(null));
             
            QTB = new VICC_QUERYTB();
        } 

        public virtual void TryLoad()
        {
            switch (JOBTYPE)
            { 
                case "JLTPLAN":
                 //   fHandle = new ViewModelBase.AsyncRunHandle(Scheduler.JLTPLANDB);
                    AsyncRun(fHandle, "JLTPLANDB", null, null, null);
                    break;
                case "JLTAPS":
                  //  fHandle = new ViewModelBase.AsyncRunHandle(Scheduler.JLTAPSDB);
                    AsyncRun(fHandle, "JLTAPSDB", null, null, null);
                    break;
                case "YGRAPS":
                    fHandle = new ViewModelBase.AsyncRunHandle(YGRAPSDB);
                    AsyncRun(fHandle, "YGRAPSDB", null, null, null);
                    break; 
                case "StartSchedule":
                   // fHandle = new ViewModelBase.AsyncRunHandle(Scheduler.StartSchedule);
                    AsyncRun(fHandle, "StartSchedule", null, null, null);
                    break;

            }
        }

        public DataSet YGRAPSDB(object _KSTKRS = null, object _KST = null, object _ABFAGANG = null, object p4 = null, object p5 = null)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            DataRow dwData = null;
            string SetDate = "";
            try
            {
                dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                VICC_PVKPOS.InsertICC_PVKPOS(dwData, 2);//INSERT

                VICC_PVKPOS.UpdateICC_PVKPOS(dwData, 2);//VKKLINES
                VICC_PVKPOS.UpdateICC_PVKPOS(dwData, 5);//股线涉及的单纱个数
                VICC_PVKPOS.UpdateICC_PVKPOS(dwData, 21);//订单行汇总

                VICC_JWYARN.InsertICC_JWYARN(dwData, 3);//如果是股线，则把细纱作为BOM的经纱
                VICC_JWYARN.InsertICC_JWYARN(dwData, 4);//如果本身是单纱，则生产一条纬纱记录

                VICC_JWYARN.UpdateICC_JWYARN(dwData, 2);//随机生成RGB

                dwData = QTB.SetProd.Tables["ICC_QUERYTB"].NewRow();
                dwData["ABFMANDANT"] = app.Mandant;
                dwData["ABFKSTKRS"] = (string)_KSTKRS;
                dwData["ABFKST"] = (string)_KST;
                dwData["MKFERLKZ"] = " ";
                dwData["ABFAGANG"] = (string)_ABFAGANG;
                INTEXDBC.DeleteMKRF(dwData, 2);

                INTEXDBC.DeleteICC_MKRF(dwData, 2);

                VICC_MKRF.InsertICC_MKRF(dwData, 2);//插入待排产记录 

                VICC_MKRF.UpdateICC_MKRF(dwData, 4);//更新纱支等信息

                VICC_MKRF.UpdateICC_MKRF(dwData, 5);//数量更新

                VICC_MKRF.UpdateICC_MKRF(dwData, 6);//加工速度
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }   
        
        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                //Scheduler.ShowMachine();
                if (vwMKRF != null)
                {
                    vwMKRF.MoveCurrentToFirst(); 
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
     
        public virtual void LoadVKPList()
        {
            try
            {
               
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
 
        //寻找出所有的生产单号码，可以一次得到
       
        public virtual void GetOrderNetWork(string _krs, string _lfd, string _pos)
        {


        }

        public virtual void BuildData(DataRow dwInput)
        {
            try
            {
               

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void EditJob()
        {
            /*
            if (dwMKRF == null)
            {
                MessageBox.Show("请选择任务");
                return;
            }
            string JobID = "Job" + dwMKRF["ABAID"].ToString();
            UIJOB job = (UIJOB)JobsLib[JobID];
            JobDetailWindow JobDetailWindow = new JobDetailWindow();
            JobDetailWindow.DataContext = job.DataContext;
            JobDetailWindow.Show();
            */
        }

        public void RemoveAllJobs()
        {
            /*
            foreach (object obj in MGANTTSLib.Values)
            {
                MGANTT machine = (MGANTT)obj;
                if (machine != null)
                {
                    int count = machine.MainGrid.Children.Count;
                    if (count > 1)
                    {
                        machine.MainGrid.Children.RemoveRange(1, count - 1);
                    }
                }
            }*/
        }
    }
}